package com.weilive.core.utils;

import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.alibaba.fastjson.JSONObject;

public class StringUtils {
	
	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
	
	private static DateFormat date_format = new SimpleDateFormat("yyyy/M/d", Locale.ENGLISH);
	private static DateFormat time_format = new SimpleDateFormat("yyyy/M/d hh:mm:ss", Locale.ENGLISH);
	
	public static String defFormatDate(Date date) {
		return date_format.format(date);
	}
	
	public static String defFormatTime(Date date) {
		return time_format.format(date);
	}

	public static String firstUpcase(String text) {
		if (text != null && text.length() > 0) {
			if (text.length() == 1) {
				return text.toUpperCase();
			}
			return text.substring(0, 1).toUpperCase() + text.substring(1); 
		}
		return text;
	}
	
	public static String getter(String field) {
		return "get" + firstUpcase(field);
	}
	
	public static String setter(String field) {
		return "set" + firstUpcase(field);
	}
	
	public static String indent(int idx) {
		StringBuffer result = new StringBuffer(idx*4 + 1);
		for (int i = 0; i < idx; i ++) {
			result.append("    ");
		}
		return result.toString();
	}
	
	/**
	 * Parse1
	 */
	
	public static Boolean parseBoolean(String s) {
		try {
			return Boolean.parseBoolean(s);
		} catch (Exception e) {
		}
		return null;
	}
	
	public static Integer parseInt(String s) {
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
		}
		return null;
	}
	
	public static Long parseLong(String s) {
		try {
			return Long.parseLong(s);
		} catch (Exception e) {
		}
		return null;
	}
	
	public static Float parseFloat(String s) {
		try {
			return Float.parseFloat(s);
		} catch (Exception e) {
		}
		return null;
	}
	
	public static Double parseDouble(String s) {
		try {
			return Double.parseDouble(s);
		} catch (Exception e) {
		}
		return null;
	}
	
	/**
	 * Parse2
	 */
	
	public static boolean parseBoolean(String s, boolean def) {
		try {
			return Boolean.parseBoolean(s);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static int parseInt(String s, int def) {
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static long parseLong(String s, long def) {
		try {
			return Long.parseLong(s);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static float parseFloat(String s, float def) {
		try {
			return Float.parseFloat(s);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static double parseDouble(String s, double def) {
		try {
			return Double.parseDouble(s);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static int[] parseVersion(String version) {
		int[] ret = {0,0,0};
		if (version != null) {
			String[] nums = version.replace('.', '-').split("-");
			for (int i = 0; i < nums.length && i < ret.length; i ++) {
				ret[i] = parseInt(nums[i], 0);
			}
		}
		return ret;
	}
	
	public static String toString(Object o) {
		return JSONObject.toJSONString(o);
	}
	
	public static boolean isNull(String str)
	{
	    return str == null || str.trim().length() == 0;
	}
}
